package com.example.mongodb.demo.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.mongodb.demo.dao.ShopDao;
import com.example.mongodb.demo.dao.ShopRepository;
import com.example.mongodb.demo.model.entity.Goods;
import com.example.mongodb.demo.model.entity.Shop;
import com.example.mongodb.demo.service.IShopService;
import com.mongodb.BasicDBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.LookupOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopServiceImpl implements IShopService {

    @Autowired
    private ShopDao shopDao;
    @Autowired
    private ShopRepository shopRepository;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Shop findById(String id) {
        return shopDao.findById(id);
    }

    @Override
    public List<BasicDBObject> findGoodsByShopId(String shopId, Pageable pageable) {
        //拼装关联信息
        LookupOperation lookupOperation = Aggregation.lookup("goods",//关联表名
                "_id",//关联字段
                "shopId",//主表关联字段对应的次表字段
                "shopGoods");//查询结果集合名
        //拼装具体查询信息
        Criteria criteria1 = new Criteria();
        //criteria1 = Criteria.where("isOpen").is(1);
        AggregationOperation match1 = Aggregation.match(criteria1);

        Criteria criteria2 = new Criteria();
        //criteria2 = Criteria.where("shopId").is(shopId);
        AggregationOperation match2 = Aggregation.match(criteria2);

        //分页查询
        Aggregation aggregation = Aggregation.newAggregation(match1, lookupOperation, match2, Aggregation.sort(pageable.getSort()),//排序
                Aggregation.skip(pageable.getPageNumber() > 1 ? (pageable.getPageNumber() - 1) * pageable.getPageSize() : 0),
                Aggregation.limit(pageable.getPageSize()));
        //总数查询
        Aggregation counts = Aggregation.newAggregation(match1, lookupOperation, match2);

        int count = mongoTemplate.aggregate(counts, "shop", BasicDBObject.class).getMappedResults().size();
        List<BasicDBObject> results = mongoTemplate.aggregate(aggregation, "shop", BasicDBObject.class).getMappedResults();
        return results;
    }

    @Override
    public Shop save(Shop blog) {
        return shopDao.save(blog);
    }

    @Override
    public void delete(Shop blog) {
        shopDao.delete(blog);
    }

    @Override
    public long update(Shop blog) {
        return shopDao.update(blog);
    }

    @Override
    public List<Shop> findByShopName(String shopName) {
        return shopDao.findByShopName(shopName);
    }

    @Override
    public List<Shop> findByIsOpen(int isOpen) {
        return shopRepository.findByIsOpen(isOpen);
    }

    @Override
    public Page<Shop> findAll(Shop blog, Pageable pageable) {
        return shopDao.findAll(blog,pageable);
    }

}
